About this dataset :
Customer Personality Analysis is a detailed analysis of a company’s ideal customers. It helps a business to better understand its customers and makes it easier for them to modify products according to the specific needs, behaviors and concerns of different types of customers.
Customer personality analysis helps a business to modify its product based on its target customers from different types of customer segments. For example, instead of spending money to market a new product to every customer in the company’s database, a company can analyze which customer segment is most likely to buy the product and then market the product only on that particular segment.
Attributes
Customer Information
ID : Customer’s unique identifier
Year_Birth : Customer’s birth year
Education : Customer’s education level
Marital_Status: Customer’s marital status
Income : Customer’s yearly household income
Kidhome : Number of children in customer’s household
Teenhome : Number of teenagers in customer’s household
Dt_Customer : Date of customer’s enrollment with the company
Recency : Number of days since customer’s last purchase
Complain : 1 if the customer complained in the last 2 years, 0 otherwise
Products
MntWines : Amount spent on wine in last 2 years
MntFruits : Amount spent on fruits in last 2 years
MntMeatProducts : Amount spent on meat in last 2 years
MntFishProducts : Amount spent on fish in last 2 years
MntSweetProducts: Amount spent on sweets in last 2 years
MntGoldProds : Amount spent on gold in last 2 years
Promotion
NumDealsPurchases: Number of purchases made with a discount
AcceptedCmp1 : 1 if customer accepted the offer in the 1st campaign, 0 otherwise
AcceptedCmp2 : 1 if customer accepted the offer in the 2nd campaign, 0 otherwise
AcceptedCmp3 : 1 if customer accepted the offer in the 3rd campaign, 0 otherwise
AcceptedCmp4 : 1 if customer accepted the offer in the 4th campaign, 0 otherwise
AcceptedCmp5 : 1 if customer accepted the offer in the 5th campaign, 0 otherwise
Response : 1 if customer accepted the offer in the last campaign, 0 otherwise
Place
NumWebPurchases : Number of purchases made through the company’s website
NumCatalogPurchases: Number of purchases made using a catalogue
NumStorePurchases : Number of purchases made directly in stores
NumWebVisitsMonth : Number of visits to company’s website in the last month
Dataset Source: Kaggle : https://www.kaggle.com/imakash3011/customer-personality-analysis
# Load libraries
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.1 ──
## ✓ ggplot2 3.3.5 ✓ purrr 0.3.4
## ✓ tibble 3.1.6 ✓ dplyr 1.0.7
## ✓ tidyr 1.1.4 ✓ stringr 1.4.0
## ✓ readr 2.1.1 ✓ forcats 0.5.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
library(corrplot)
## corrplot 0.92 loaded
library(gridExtra)
##
## Attaching package: 'gridExtra'
## The following object is masked from 'package:dplyr':
##
## combine
library(GGally)
## Registered S3 method overwritten by 'GGally':
## method from
## +.gg ggplot2
library(knitr)
library(ggplot2)
# create a dataframe and store as variable df
options(repr.matrix.max.cols=30, repr.matrix.max.rows=30) # to show all column
cust <- read.csv('marketing_campaign.csv')
# display top 6 first rows
head(cust)
## ID Year_Birth Education Marital_Status Income Kidhome Teenhome Dt_Customer
## 1 5524 1957 Graduation Single 58138 0 0 04/09/2012
## 2 2174 1954 Graduation Single 46344 1 1 08/03/2014
## 3 4141 1965 Graduation Together 71613 0 0 21/08/2013
## 4 6182 1984 Graduation Together 26646 1 0 10/02/2014
## 5 5324 1981 PhD Married 58293 1 0 19/01/2014
## 6 7446 1967 Master Together 62513 0 1 09/09/2013
## Recency MntWines MntFruits MntMeatProducts MntFishProducts MntSweetProducts
## 1 58 635 88 546 172 88
## 2 38 11 1 6 2 1
## 3 26 426 49 127 111 21
## 4 26 11 4 20 10 3
## 5 94 173 43 118 46 27
## 6 16 520 42 98 0 42
## MntGoldProds NumDealsPurchases NumWebPurchases NumCatalogPurchases
## 1 88 3 8 10
## 2 6 2 1 1
## 3 42 1 8 2
## 4 5 2 2 0
## 5 15 5 5 3
## 6 14 2 6 4
## NumStorePurchases NumWebVisitsMonth AcceptedCmp3 AcceptedCmp4 AcceptedCmp5
## 1 4 7 0 0 0
## 2 2 5 0 0 0
## 3 10 4 0 0 0
## 4 4 6 0 0 0
## 5 6 5 0 0 0
## 6 10 6 0 0 0
## AcceptedCmp1 AcceptedCmp2 Complain Response
## 1 0 0 0 1
## 2 0 0 0 0
## 3 0 0 0 0
## 4 0 0 0 0
## 5 0 0 0 0
## 6 0 0 0 0
# display bottom 6 last rows
tail(cust)
## ID Year_Birth Education Marital_Status Income Kidhome Teenhome
## 2235 8372 1974 Graduation Married 34421 1 0
## 2236 10870 1967 Graduation Married 61223 0 1
## 2237 4001 1946 PhD Together 64014 2 1
## 2238 7270 1981 Graduation Divorced 56981 0 0
## 2239 8235 1956 Master Together 69245 0 1
## 2240 9405 1954 PhD Married 52869 1 1
## Dt_Customer Recency MntWines MntFruits MntMeatProducts MntFishProducts
## 2235 01/07/2013 81 3 3 7 6
## 2236 13/06/2013 46 709 43 182 42
## 2237 10/06/2014 56 406 0 30 0
## 2238 25/01/2014 91 908 48 217 32
## 2239 24/01/2014 8 428 30 214 80
## 2240 15/10/2012 40 84 3 61 2
## MntSweetProducts MntGoldProds NumDealsPurchases NumWebPurchases
## 2235 2 9 1 1
## 2236 118 247 2 9
## 2237 0 8 7 8
## 2238 12 24 1 2
## 2239 30 61 2 6
## 2240 1 21 3 3
## NumCatalogPurchases NumStorePurchases NumWebVisitsMonth AcceptedCmp3
## 2235 0 2 7 0
## 2236 3 4 5 0
## 2237 2 5 7 0
## 2238 3 13 6 0
## 2239 5 10 3 0
## 2240 1 4 7 0
## AcceptedCmp4 AcceptedCmp5 AcceptedCmp1 AcceptedCmp2 Complain Response
## 2235 0 0 0 0 0 0
## 2236 0 0 0 0 0 0
## 2237 0 0 1 0 0 0
## 2238 1 0 0 0 0 0
## 2239 0 0 0 0 0 0
## 2240 0 0 0 0 0 1
str(cust)
## 'data.frame': 2240 obs. of 27 variables:
## $ ID : int 5524 2174 4141 6182 5324 7446 965 6177 4855 5899 ...
## $ Year_Birth : int 1957 1954 1965 1984 1981 1967 1971 1985 1974 1950 ...
## $ Education : chr "Graduation" "Graduation" "Graduation" "Graduation" ...
## $ Marital_Status : chr "Single" "Single" "Together" "Together" ...
## $ Income : int 58138 46344 71613 26646 58293 62513 55635 33454 30351 5648 ...
## $ Kidhome : int 0 1 0 1 1 0 0 1 1 1 ...
## $ Teenhome : int 0 1 0 0 0 1 1 0 0 1 ...
## $ Dt_Customer : chr "04/09/2012" "08/03/2014" "21/08/2013" "10/02/2014" ...
## $ Recency : int 58 38 26 26 94 16 34 32 19 68 ...
## $ MntWines : int 635 11 426 11 173 520 235 76 14 28 ...
## $ MntFruits : int 88 1 49 4 43 42 65 10 0 0 ...
## $ MntMeatProducts : int 546 6 127 20 118 98 164 56 24 6 ...
## $ MntFishProducts : int 172 2 111 10 46 0 50 3 3 1 ...
## $ MntSweetProducts : int 88 1 21 3 27 42 49 1 3 1 ...
## $ MntGoldProds : int 88 6 42 5 15 14 27 23 2 13 ...
## $ NumDealsPurchases : int 3 2 1 2 5 2 4 2 1 1 ...
## $ NumWebPurchases : int 8 1 8 2 5 6 7 4 3 1 ...
## $ NumCatalogPurchases: int 10 1 2 0 3 4 3 0 0 0 ...
## $ NumStorePurchases : int 4 2 10 4 6 10 7 4 2 0 ...
## $ NumWebVisitsMonth : int 7 5 4 6 5 6 6 8 9 20 ...
## $ AcceptedCmp3 : int 0 0 0 0 0 0 0 0 0 1 ...
## $ AcceptedCmp4 : int 0 0 0 0 0 0 0 0 0 0 ...
## $ AcceptedCmp5 : int 0 0 0 0 0 0 0 0 0 0 ...
## $ AcceptedCmp1 : int 0 0 0 0 0 0 0 0 0 0 ...
## $ AcceptedCmp2 : int 0 0 0 0 0 0 0 0 0 0 ...
## $ Complain : int 0 0 0 0 0 0 0 0 0 0 ...
## $ Response : int 1 0 0 0 0 0 0 0 1 0 ...
Summary : 1. The dataset contains 27 variables.
2. Variable Dt_Customer was supposed to in DateTime type.
3. There are 16 numerical variable, 11 categorical variables.
4. We can extract the age of the customer using variable Year_Birth.
5. We can create variable children from addition of Kidhome and Teenhome.
6. We can also create the total spend of the products that customers bought, through sum of product that they bought.
7. We can extract the total of the days since customer start shopping in the grocery
summary(cust)
## ID Year_Birth Education Marital_Status
## Min. : 0 Min. :1893 Length:2240 Length:2240
## 1st Qu.: 2828 1st Qu.:1959 Class :character Class :character
## Median : 5458 Median :1970 Mode :character Mode :character
## Mean : 5592 Mean :1969
## 3rd Qu.: 8428 3rd Qu.:1977
## Max. :11191 Max. :1996
##
## Income Kidhome Teenhome Dt_Customer
## Min. : 1730 Min. :0.0000 Min. :0.0000 Length:2240
## 1st Qu.: 35303 1st Qu.:0.0000 1st Qu.:0.0000 Class :character
## Median : 51382 Median :0.0000 Median :0.0000 Mode :character
## Mean : 52247 Mean :0.4442 Mean :0.5062
## 3rd Qu.: 68522 3rd Qu.:1.0000 3rd Qu.:1.0000
## Max. :666666 Max. :2.0000 Max. :2.0000
## NA's :24
## Recency MntWines MntFruits MntMeatProducts
## Min. : 0.00 Min. : 0.00 Min. : 0.0 Min. : 0
## 1st Qu.:24.00 1st Qu.: 23.75 1st Qu.: 1.0 1st Qu.: 16
## Median :49.00 Median : 173.50 Median : 8.0 Median : 67
## Mean :49.11 Mean : 303.94 Mean : 26.3 Mean : 167
## 3rd Qu.:74.00 3rd Qu.: 504.25 3rd Qu.: 33.0 3rd Qu.: 232
## Max. :99.00 Max. :1493.00 Max. :199.0 Max. :1725
##
## MntFishProducts MntSweetProducts MntGoldProds NumDealsPurchases
## Min. : 0.00 Min. : 0.00 Min. : 0.00 Min. : 0.000
## 1st Qu.: 3.00 1st Qu.: 1.00 1st Qu.: 9.00 1st Qu.: 1.000
## Median : 12.00 Median : 8.00 Median : 24.00 Median : 2.000
## Mean : 37.53 Mean : 27.06 Mean : 44.02 Mean : 2.325
## 3rd Qu.: 50.00 3rd Qu.: 33.00 3rd Qu.: 56.00 3rd Qu.: 3.000
## Max. :259.00 Max. :263.00 Max. :362.00 Max. :15.000
##
## NumWebPurchases NumCatalogPurchases NumStorePurchases NumWebVisitsMonth
## Min. : 0.000 Min. : 0.000 Min. : 0.00 Min. : 0.000
## 1st Qu.: 2.000 1st Qu.: 0.000 1st Qu.: 3.00 1st Qu.: 3.000
## Median : 4.000 Median : 2.000 Median : 5.00 Median : 6.000
## Mean : 4.085 Mean : 2.662 Mean : 5.79 Mean : 5.317
## 3rd Qu.: 6.000 3rd Qu.: 4.000 3rd Qu.: 8.00 3rd Qu.: 7.000
## Max. :27.000 Max. :28.000 Max. :13.00 Max. :20.000
##
## AcceptedCmp3 AcceptedCmp4 AcceptedCmp5 AcceptedCmp1
## Min. :0.00000 Min. :0.00000 Min. :0.00000 Min. :0.00000
## 1st Qu.:0.00000 1st Qu.:0.00000 1st Qu.:0.00000 1st Qu.:0.00000
## Median :0.00000 Median :0.00000 Median :0.00000 Median :0.00000
## Mean :0.07277 Mean :0.07455 Mean :0.07277 Mean :0.06429
## 3rd Qu.:0.00000 3rd Qu.:0.00000 3rd Qu.:0.00000 3rd Qu.:0.00000
## Max. :1.00000 Max. :1.00000 Max. :1.00000 Max. :1.00000
##
## AcceptedCmp2 Complain Response
## Min. :0.00000 Min. :0.000000 Min. :0.0000
## 1st Qu.:0.00000 1st Qu.:0.000000 1st Qu.:0.0000
## Median :0.00000 Median :0.000000 Median :0.0000
## Mean :0.01339 Mean :0.009375 Mean :0.1491
## 3rd Qu.:0.00000 3rd Qu.:0.000000 3rd Qu.:0.0000
## Max. :1.00000 Max. :1.000000 Max. :1.0000
##
Summary : 1. Based on the summary above, there is a varible that contains 24 missing value in variable Income 2. And also the values from all varibles are not in the same scale, so they should be transformed into the same scale before performing the clustering.
# Check the unique element in variable Education
table(cust$Education)
##
## 2n Cycle Basic Graduation Master PhD
## 203 54 1127 370 486
# Check the unique element in variable Marital Status
table(cust$Marital_Status)
##
## Absurd Alone Divorced Married Single Together Widow YOLO
## 2 3 232 864 480 580 77 2
Summary : We can derive the new varible from variable Eduation and Marital Status
Since the total of missing values are only around 1% of the data, we just drop it.
# check the total of missing values
sum(is.na(cust))
## [1] 24
# create a copy of original data
cln <- data.frame(cust)
# remove missing values
cln <- na.omit(cln)
# Check after remove the missing value
sum(is.na(cln))
## [1] 0
# Boxplot for each Attribute
# Subset the dataframe without categorical feature, feature ID and Dt_Customer
col_names <- colnames(cln[c(2,5:7,9:20)])
# create loop to plot boxplot
for (i in col_names){
boxplot <- ggplot(cln, aes_string(y=i)) +
geom_boxplot(fill="#69b3a2")
print(boxplot)
}
# define function to check the outliers using IQR
FindOutliers <- function(data) {
lowerq = quantile(data)[2]
upperq = quantile(data)[4]
iqr = upperq - lowerq #Or use IQR(data)
# we identify extreme outliers
extreme.threshold.upper = (iqr * 1.5) + upperq
extreme.threshold.lower = lowerq - (iqr * 1.5)
result <- which(data > extreme.threshold.upper | data < extreme.threshold.lower)
length(result)}
apply(cln[c(2,5:7,9:20)], 2, FindOutliers)
## Year_Birth Income Kidhome Teenhome
## 3 8 0 0
## Recency MntWines MntFruits MntMeatProducts
## 0 35 246 174
## MntFishProducts MntSweetProducts MntGoldProds NumDealsPurchases
## 222 246 205 84
## NumWebPurchases NumCatalogPurchases NumStorePurchases NumWebVisitsMonth
## 3 23 0 8
# check summary after treat missing values
summary(cln)
## ID Year_Birth Education Marital_Status
## Min. : 0 Min. :1893 Length:2216 Length:2216
## 1st Qu.: 2815 1st Qu.:1959 Class :character Class :character
## Median : 5458 Median :1970 Mode :character Mode :character
## Mean : 5588 Mean :1969
## 3rd Qu.: 8422 3rd Qu.:1977
## Max. :11191 Max. :1996
## Income Kidhome Teenhome Dt_Customer
## Min. : 1730 Min. :0.0000 Min. :0.0000 Length:2216
## 1st Qu.: 35303 1st Qu.:0.0000 1st Qu.:0.0000 Class :character
## Median : 51382 Median :0.0000 Median :0.0000 Mode :character
## Mean : 52247 Mean :0.4418 Mean :0.5054
## 3rd Qu.: 68522 3rd Qu.:1.0000 3rd Qu.:1.0000
## Max. :666666 Max. :2.0000 Max. :2.0000
## Recency MntWines MntFruits MntMeatProducts
## Min. : 0.00 Min. : 0.0 Min. : 0.00 Min. : 0.0
## 1st Qu.:24.00 1st Qu.: 24.0 1st Qu.: 2.00 1st Qu.: 16.0
## Median :49.00 Median : 174.5 Median : 8.00 Median : 68.0
## Mean :49.01 Mean : 305.1 Mean : 26.36 Mean : 167.0
## 3rd Qu.:74.00 3rd Qu.: 505.0 3rd Qu.: 33.00 3rd Qu.: 232.2
## Max. :99.00 Max. :1493.0 Max. :199.00 Max. :1725.0
## MntFishProducts MntSweetProducts MntGoldProds NumDealsPurchases
## Min. : 0.00 Min. : 0.00 Min. : 0.00 Min. : 0.000
## 1st Qu.: 3.00 1st Qu.: 1.00 1st Qu.: 9.00 1st Qu.: 1.000
## Median : 12.00 Median : 8.00 Median : 24.50 Median : 2.000
## Mean : 37.64 Mean : 27.03 Mean : 43.97 Mean : 2.324
## 3rd Qu.: 50.00 3rd Qu.: 33.00 3rd Qu.: 56.00 3rd Qu.: 3.000
## Max. :259.00 Max. :262.00 Max. :321.00 Max. :15.000
## NumWebPurchases NumCatalogPurchases NumStorePurchases NumWebVisitsMonth
## Min. : 0.000 Min. : 0.000 Min. : 0.000 Min. : 0.000
## 1st Qu.: 2.000 1st Qu.: 0.000 1st Qu.: 3.000 1st Qu.: 3.000
## Median : 4.000 Median : 2.000 Median : 5.000 Median : 6.000
## Mean : 4.085 Mean : 2.671 Mean : 5.801 Mean : 5.319
## 3rd Qu.: 6.000 3rd Qu.: 4.000 3rd Qu.: 8.000 3rd Qu.: 7.000
## Max. :27.000 Max. :28.000 Max. :13.000 Max. :20.000
## AcceptedCmp3 AcceptedCmp4 AcceptedCmp5 AcceptedCmp1
## Min. :0.00000 Min. :0.00000 Min. :0.0000 Min. :0.00000
## 1st Qu.:0.00000 1st Qu.:0.00000 1st Qu.:0.0000 1st Qu.:0.00000
## Median :0.00000 Median :0.00000 Median :0.0000 Median :0.00000
## Mean :0.07356 Mean :0.07401 Mean :0.0731 Mean :0.06408
## 3rd Qu.:0.00000 3rd Qu.:0.00000 3rd Qu.:0.0000 3rd Qu.:0.00000
## Max. :1.00000 Max. :1.00000 Max. :1.0000 Max. :1.00000
## AcceptedCmp2 Complain Response
## Min. :0.00000 Min. :0.000000 Min. :0.0000
## 1st Qu.:0.00000 1st Qu.:0.000000 1st Qu.:0.0000
## Median :0.00000 Median :0.000000 Median :0.0000
## Mean :0.01354 Mean :0.009477 Mean :0.1503
## 3rd Qu.:0.00000 3rd Qu.:0.000000 3rd Qu.:0.0000
## Max. :1.00000 Max. :1.000000 Max. :1.0000
From the box plot, outliers calculation using IQR method, and the summary of data, we can identify that there are around 50% outliers in this data. But the outliers here are basically the natural value. It is possible if the customer buy many wines, fishe, and etc. However for the income of 666666 might be the error data. Furthermore, there are also some outliers in feature Year_birth, It might be possible to have age of above 100, but according to average of human life, it’s not make sense. So we are gonna treat the outliers in feature income and Year_birth through removing the outliers, since the outliers are only 11.
# Removing the outliers in feature income
# create a variale which contains Q1 & Q3
Q.inc <- quantile(cln$Income, probs=c(.25, .75), na.rm = FALSE)
# calculate IQR
IQR.inc <- IQR(cln$Income)
# find the cut-off ranges beyond which all data points are outliers.
ub.inc <- (IQR.inc * 1.5) + Q.inc[2]
lb.inc <- Q.inc[1] - (IQR.inc * 1.5)
# extract the part of dataframe which isn't included the outlier values
cln <- subset(cln, cln$Income > lb.inc & cln$Income < ub.inc)
# Removing the outliers in feature Year_Birth
# create a variale which contains Q1 & Q3
Q.yb <- quantile(cln$Year_Birth, probs=c(.25, .75), na.rm = FALSE)
# calculate IQR
IQR.yb <- IQR(cln$Year_Birth)
# find the cut-off ranges beyond which all data points are outliers.
ub.yb <- (IQR.yb * 1.5) + Q.yb[2]
lb.yb <- Q.yb[1] - (IQR.yb * 1.5)
# extract the part of dataframe which isn't included the outlier values
cln <- subset(cln, cln$Year_Birth > lb.yb & cln$Year_Birth < ub.yb)
# Check the outlier in feature Income and Year_Birth after remove it
apply(cln[c(2,5)], 2, FindOutliers)
## Year_Birth Income
## 0 0
We can see that no outliers exist. It works !
After we finish clean the data, now there are some possibility to derive the new features from the existing features.
# Extract the age of the customer
cln$Age <- 2022 - cln$Year_Birth
# Derive spent variable
cln$Total_Spent <- cln$MntFishProducts + cln$MntFruits + cln$MntGoldProds + cln$MntMeatProducts + cln$MntSweetProducts + cln$MntWines
# Derive variable total children in the family
cln$children <- cln$Kidhome + cln$Teenhome
# replace the value of some columns
# Education
cln$Education[cln$Education == "2n Cycle"] <- "undergraduate"
cln$Education[cln$Education == "Basic"] <- "undergraduate"
cln$Education[cln$Education == "Graduation"] <- "postgraduate"
cln$Education[cln$Education == "Master"] <- "postgraduate"
cln$Education[cln$Education == "PhD"] <- "postgraduate"
# Marital Status
cln$Marital_Status[cln$Marital_Status == "single"] <- "Single"
cln$Marital_Status[cln$Marital_Status == "Absurd"] <- "Single"
cln$Marital_Status[cln$Marital_Status == "Alone"] <- "Single"
cln$Marital_Status[cln$Marital_Status == "Together"] <- "Married"
cln$Marital_Status[cln$Marital_Status == "Widow"] <- "Divorced"
cln$Marital_Status[cln$Marital_Status == "YOLO"] <- "Single"
# change some columns' name
names(cln)[names(cln) == "MntWines"] <- "Wines"
names(cln)[names(cln) == "MntFruits"] <- "Fruits"
names(cln)[names(cln) == "MntSweetProducts"] <- "Sweet"
names(cln)[names(cln) == "MntMeatProducts"] <- "Meat"
names(cln)[names(cln) == "MntGoldProds"] <- "Gold"
names(cln)[names(cln) == "MntFishProducts"] <- "Fish"
names(cln)
## [1] "ID" "Year_Birth" "Education"
## [4] "Marital_Status" "Income" "Kidhome"
## [7] "Teenhome" "Dt_Customer" "Recency"
## [10] "Wines" "Fruits" "Meat"
## [13] "Fish" "Sweet" "Gold"
## [16] "NumDealsPurchases" "NumWebPurchases" "NumCatalogPurchases"
## [19] "NumStorePurchases" "NumWebVisitsMonth" "AcceptedCmp3"
## [22] "AcceptedCmp4" "AcceptedCmp5" "AcceptedCmp1"
## [25] "AcceptedCmp2" "Complain" "Response"
## [28] "Age" "Total_Spent" "children"
# FIX DATASET
clean_fe <- cln[, c("Age","Education","Marital_Status","children","Income","Recency","Wines","Fruits","Meat","Fish","Sweet","Gold",
"Total_Spent","NumDealsPurchases","NumWebPurchases","NumCatalogPurchases","NumStorePurchases","NumWebVisitsMonth",
"AcceptedCmp3","AcceptedCmp4","AcceptedCmp5","AcceptedCmp1","AcceptedCmp2","Complain","Response")]
# change all columns' name to lower case letter
names(clean_fe)<- tolower(names(clean_fe))
summary(clean_fe)
## age education marital_status children
## Min. :26.0 Length:2205 Length:2205 Min. :0.0000
## 1st Qu.:45.0 Class :character Class :character 1st Qu.:0.0000
## Median :52.0 Mode :character Mode :character Median :1.0000
## Mean :53.1 Mean :0.9488
## 3rd Qu.:63.0 3rd Qu.:1.0000
## Max. :82.0 Max. :3.0000
## income recency wines fruits
## Min. : 1730 Min. : 0.00 Min. : 0.0 Min. : 0.0
## 1st Qu.: 35196 1st Qu.:24.00 1st Qu.: 24.0 1st Qu.: 2.0
## Median : 51287 Median :49.00 Median : 178.0 Median : 8.0
## Mean : 51622 Mean :49.01 Mean : 306.2 Mean : 26.4
## 3rd Qu.: 68281 3rd Qu.:74.00 3rd Qu.: 507.0 3rd Qu.: 33.0
## Max. :113734 Max. :99.00 Max. :1493.0 Max. :199.0
## meat fish sweet gold
## Min. : 0.0 Min. : 0.00 Min. : 0.00 Min. : 0.00
## 1st Qu.: 16.0 1st Qu.: 3.00 1st Qu.: 1.00 1st Qu.: 9.00
## Median : 68.0 Median : 12.00 Median : 8.00 Median : 25.00
## Mean : 165.3 Mean : 37.76 Mean : 27.13 Mean : 44.06
## 3rd Qu.: 232.0 3rd Qu.: 50.00 3rd Qu.: 34.00 3rd Qu.: 56.00
## Max. :1725.0 Max. :259.00 Max. :262.00 Max. :321.00
## total_spent numdealspurchases numwebpurchases numcatalogpurchases
## Min. : 5.0 Min. : 0.000 Min. : 0.000 Min. : 0.000
## 1st Qu.: 69.0 1st Qu.: 1.000 1st Qu.: 2.000 1st Qu.: 0.000
## Median : 397.0 Median : 2.000 Median : 4.000 Median : 2.000
## Mean : 606.8 Mean : 2.318 Mean : 4.101 Mean : 2.645
## 3rd Qu.:1047.0 3rd Qu.: 3.000 3rd Qu.: 6.000 3rd Qu.: 4.000
## Max. :2525.0 Max. :15.000 Max. :27.000 Max. :28.000
## numstorepurchases numwebvisitsmonth acceptedcmp3 acceptedcmp4
## Min. : 0.000 Min. : 0.000 Min. :0.00000 Min. :0.00000
## 1st Qu.: 3.000 1st Qu.: 3.000 1st Qu.:0.00000 1st Qu.:0.00000
## Median : 5.000 Median : 6.000 Median :0.00000 Median :0.00000
## Mean : 5.824 Mean : 5.337 Mean :0.07392 Mean :0.07438
## 3rd Qu.: 8.000 3rd Qu.: 7.000 3rd Qu.:0.00000 3rd Qu.:0.00000
## Max. :13.000 Max. :20.000 Max. :1.00000 Max. :1.00000
## acceptedcmp5 acceptedcmp1 acceptedcmp2 complain
## Min. :0.00000 Min. :0.0000 Min. :0.00000 Min. :0.00000
## 1st Qu.:0.00000 1st Qu.:0.0000 1st Qu.:0.00000 1st Qu.:0.00000
## Median :0.00000 Median :0.0000 Median :0.00000 Median :0.00000
## Mean :0.07302 Mean :0.0644 Mean :0.01361 Mean :0.00907
## 3rd Qu.:0.00000 3rd Qu.:0.0000 3rd Qu.:0.00000 3rd Qu.:0.00000
## Max. :1.00000 Max. :1.0000 Max. :1.00000 Max. :1.00000
## response
## Min. :0.000
## 1st Qu.:0.000
## Median :0.000
## Mean :0.151
## 3rd Qu.:0.000
## Max. :1.000
# Group the feature based on data type to ease the visualization
# numerical data
nums <- clean_fe[, c("age","children","income","recency","wines","fruits","meat","fish","sweet","gold","total_spent",
"numdealspurchases","numwebpurchases","numcatalogpurchases","numstorepurchases","numwebvisitsmonth",
"acceptedcmp3","acceptedcmp4","acceptedcmp5","acceptedcmp1","acceptedcmp2","complain","response")]
#categorical data
cats <- clean_fe[, c("education","marital_status")]
# Density plot for each attribute
col_nums <- colnames(nums)
for (i in col_nums){
density <- ggplot(nums, aes_string(x=i)) +
geom_density(fill="#69b3a2")
print(density)
}
# load package
library(corrplot)
# create correlation plot
corr <- cor(nums)
corrplot(corr, type="upper", method="ellipse", tl.cex=0.9)
# Check the unique element in variable Marital Status
table(clean_fe$marital_status)
##
## Divorced Married Single
## 306 1422 477
# Check the unique element in variable Education
table(clean_fe$education)
##
## postgraduate undergraduate
## 1953 252
head(clean_fe)
## age education marital_status children income recency wines fruits meat
## 1 65 postgraduate Single 0 58138 58 635 88 546
## 2 68 postgraduate Single 2 46344 38 11 1 6
## 3 57 postgraduate Married 0 71613 26 426 49 127
## 4 38 postgraduate Married 1 26646 26 11 4 20
## 5 41 postgraduate Married 1 58293 94 173 43 118
## 6 55 postgraduate Married 1 62513 16 520 42 98
## fish sweet gold total_spent numdealspurchases numwebpurchases
## 1 172 88 88 1617 3 8
## 2 2 1 6 27 2 1
## 3 111 21 42 776 1 8
## 4 10 3 5 53 2 2
## 5 46 27 15 422 5 5
## 6 0 42 14 716 2 6
## numcatalogpurchases numstorepurchases numwebvisitsmonth acceptedcmp3
## 1 10 4 7 0
## 2 1 2 5 0
## 3 2 10 4 0
## 4 0 4 6 0
## 5 3 6 5 0
## 6 4 10 6 0
## acceptedcmp4 acceptedcmp5 acceptedcmp1 acceptedcmp2 complain response
## 1 0 0 0 0 0 1
## 2 0 0 0 0 0 0
## 3 0 0 0 0 0 0
## 4 0 0 0 0 0 0
## 5 0 0 0 0 0 0
## 6 0 0 0 0 0 0
In this section, we will perform some data pre-processing methods to prepare the data for modeling
1. Label encoding for categorical faetures 2. Scaling the features
# import the library for label encoding
library(superml)
## Loading required package: R6
# Encoding for Feature Education
# LabelEncoder$new() creates and initializes an instance of the Label Encoder class.
label <- LabelEncoder$new()
# LabelEncoder$fit() create memory space for the encoding values but it does not return any value as an output.
print(label$fit(clean_fe$education))
## [1] TRUE
# LabelEncoder$fit_transform() encode the data as well as reserve memory for the encoding values ahead.
clean_fe$education <- label$fit_transform(clean_fe$education)
# print the result
print(clean_fe$education)
## [1] 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0
## [38] 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0
## [75] 0 0 1 0 0 0 0 0 0 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0 0 0 0 0 0 0 0
## [1999] 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0
## [2036] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0
## [2073] 1 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0
## [2110] 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 1 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0
## [2147] 0 0 0 0 1 1 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 1 0 0 1 0 0
## [2184] 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
# Encoding for Feature marital_status
# LabelEncoder$fit() create memory space for the encoding values but it does not return any value as an output.
print(label$fit(clean_fe$marital_status))
## [1] TRUE
# LabelEncoder$fit_transform() encode the data as well as reserve memory for the encoding values ahead.
clean_fe$marital_status <- label$fit_transform(clean_fe$marital_status)
# print the result
print(clean_fe$marital_status)
## [1] 0 0 1 1 1 1 2 1 1 1 1 2 2 1 0 1 1 1 0 1 1 1 1 1 0 1 1 1 2 1 1 2 1 0 1 1 2
## [38] 2 1 1 1 1 0 1 1 1 2 1 0 2 1 1 2 0 1 0 1 0 2 1 1 1 0 1 1 1 1 1 1 2 1 0 1 1
## [75] 0 1 1 1 2 1 1 0 1 1 0 1 1 2 2 1 1 0 0 1 1 1 1 1 1 0 1 1 0 1 0 1 0 1 1 1 1
## [112] 1 0 1 2 0 1 0 1 1 1 0 1 1 1 2 1 0 2 0 1 1 0 1 1 1 1 1 1 1 1 1 0 1 1 0 0 1
## [149] 0 0 1 1 2 1 2 2 0 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 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2 1 1 1 1 1 1 1 1 1 1 2 0 1 0 1 2 0 2 2 2 1
## [2110] 2 1 0 0 1 1 1 1 1 1 1 2 1 1 2 1 1 0 1 1 1 2 0 0 2 0 1 0 0 2 1 1 1 1 2 0 1
## [2147] 1 1 1 1 0 1 1 1 1 1 1 0 2 1 1 1 0 0 1 1 1 0 0 0 1 1 0 1 1 1 2 2 1 2 0 1 1
## [2184] 1 2 1 1 1 0 1 1 0 1 1 0 1 0 0 0 1 1 1 2 1 1
head(clean_fe)
## age education marital_status children income recency wines fruits meat fish
## 1 65 0 0 0 58138 58 635 88 546 172
## 2 68 0 0 2 46344 38 11 1 6 2
## 3 57 0 1 0 71613 26 426 49 127 111
## 4 38 0 1 1 26646 26 11 4 20 10
## 5 41 0 1 1 58293 94 173 43 118 46
## 6 55 0 1 1 62513 16 520 42 98 0
## sweet gold total_spent numdealspurchases numwebpurchases numcatalogpurchases
## 1 88 88 1617 3 8 10
## 2 1 6 27 2 1 1
## 3 21 42 776 1 8 2
## 4 3 5 53 2 2 0
## 5 27 15 422 5 5 3
## 6 42 14 716 2 6 4
## numstorepurchases numwebvisitsmonth acceptedcmp3 acceptedcmp4 acceptedcmp5
## 1 4 7 0 0 0
## 2 2 5 0 0 0
## 3 10 4 0 0 0
## 4 4 6 0 0 0
## 5 6 5 0 0 0
## 6 10 6 0 0 0
## acceptedcmp1 acceptedcmp2 complain response
## 1 0 0 0 1
## 2 0 0 0 0
## 3 0 0 0 0
## 4 0 0 0 0
## 5 0 0 0 0
## 6 0 0 0 0
Before applying the clustering, we have to normalize the features to be in the same range of values.
#load package
library(caret)
## Loading required package: lattice
##
## Attaching package: 'caret'
## The following object is masked from 'package:purrr':
##
## lift
# Normalization
clean_fe_norm <- data.frame(clean_fe)
# calculate the pre-process parameters from the dataset
preprocessParams <- preProcess(clean_fe_norm, method=c("range"))
# summarize transform parameters
print(preprocessParams)
## Created from 2205 samples and 25 variables
##
## Pre-processing:
## - ignored (0)
## - re-scaling to [0, 1] (25)
# transform the dataset using the parameters
clean_fe_norm <- predict(preprocessParams, clean_fe_norm)
# summarize the transformed dataset
summary(clean_fe_norm)
## age education marital_status children
## Min. :0.0000 Min. :0.0000 Min. :0.0000 Min. :0.0000
## 1st Qu.:0.3393 1st Qu.:0.0000 1st Qu.:0.5000 1st Qu.:0.0000
## Median :0.4643 Median :0.0000 Median :0.5000 Median :0.3333
## Mean :0.4839 Mean :0.1143 Mean :0.4612 Mean :0.3163
## 3rd Qu.:0.6607 3rd Qu.:0.0000 3rd Qu.:0.5000 3rd Qu.:0.3333
## Max. :1.0000 Max. :1.0000 Max. :1.0000 Max. :1.0000
## income recency wines fruits
## Min. :0.0000 Min. :0.0000 Min. :0.00000 Min. :0.00000
## 1st Qu.:0.2988 1st Qu.:0.2424 1st Qu.:0.01607 1st Qu.:0.01005
## Median :0.4425 Median :0.4949 Median :0.11922 Median :0.04020
## Mean :0.4454 Mean :0.4950 Mean :0.20507 Mean :0.13268
## 3rd Qu.:0.5942 3rd Qu.:0.7475 3rd Qu.:0.33958 3rd Qu.:0.16583
## Max. :1.0000 Max. :1.0000 Max. :1.00000 Max. :1.00000
## meat fish sweet gold
## Min. :0.000000 Min. :0.00000 Min. :0.000000 Min. :0.00000
## 1st Qu.:0.009275 1st Qu.:0.01158 1st Qu.:0.003817 1st Qu.:0.02804
## Median :0.039420 Median :0.04633 Median :0.030534 Median :0.07788
## Mean :0.095833 Mean :0.14578 Mean :0.103543 Mean :0.13725
## 3rd Qu.:0.134493 3rd Qu.:0.19305 3rd Qu.:0.129771 3rd Qu.:0.17445
## Max. :1.000000 Max. :1.00000 Max. :1.000000 Max. :1.00000
## total_spent numdealspurchases numwebpurchases numcatalogpurchases
## Min. :0.0000 Min. :0.00000 Min. :0.00000 Min. :0.00000
## 1st Qu.:0.0254 1st Qu.:0.06667 1st Qu.:0.07407 1st Qu.:0.00000
## Median :0.1556 Median :0.13333 Median :0.14815 Median :0.07143
## Mean :0.2388 Mean :0.15456 Mean :0.15188 Mean :0.09448
## 3rd Qu.:0.4135 3rd Qu.:0.20000 3rd Qu.:0.22222 3rd Qu.:0.14286
## Max. :1.0000 Max. :1.00000 Max. :1.00000 Max. :1.00000
## numstorepurchases numwebvisitsmonth acceptedcmp3 acceptedcmp4
## Min. :0.0000 Min. :0.0000 Min. :0.00000 Min. :0.00000
## 1st Qu.:0.2308 1st Qu.:0.1500 1st Qu.:0.00000 1st Qu.:0.00000
## Median :0.3846 Median :0.3000 Median :0.00000 Median :0.00000
## Mean :0.4480 Mean :0.2668 Mean :0.07392 Mean :0.07438
## 3rd Qu.:0.6154 3rd Qu.:0.3500 3rd Qu.:0.00000 3rd Qu.:0.00000
## Max. :1.0000 Max. :1.0000 Max. :1.00000 Max. :1.00000
## acceptedcmp5 acceptedcmp1 acceptedcmp2 complain
## Min. :0.00000 Min. :0.0000 Min. :0.00000 Min. :0.00000
## 1st Qu.:0.00000 1st Qu.:0.0000 1st Qu.:0.00000 1st Qu.:0.00000
## Median :0.00000 Median :0.0000 Median :0.00000 Median :0.00000
## Mean :0.07302 Mean :0.0644 Mean :0.01361 Mean :0.00907
## 3rd Qu.:0.00000 3rd Qu.:0.0000 3rd Qu.:0.00000 3rd Qu.:0.00000
## Max. :1.00000 Max. :1.0000 Max. :1.00000 Max. :1.00000
## response
## Min. :0.000
## 1st Qu.:0.000
## Median :0.000
## Mean :0.151
## 3rd Qu.:0.000
## Max. :1.000
pca_df <- data.frame(clean_fe)
pca <- prcomp(pca_df, scale. = TRUE)
# Plot variance ration in each PCA
pca_var <- pca$sdev^2
pca_var_perc <- round(pca_var/sum(pca_var) * 100, 1)
barplot(pca_var_perc, main = "Variation Plot", xlab = "PCs", ylab = "Percentage Variance", ylim = c(0, 100))
The barchart above shows that around 30% of the variation in the data is shown by PC1 and then very little is captured by the rest of the PCs. The most important features can be extract using rotation.
# Which features do contribute the most in PC1
PC1 <- pca$rotation[,1]
PC1_scores <- abs(PC1)
PC1_scores_ordered <- sort(PC1_scores, decreasing = TRUE)
names(PC1_scores_ordered)
## [1] "total_spent" "income" "numcatalogpurchases"
## [4] "meat" "wines" "numstorepurchases"
## [7] "fish" "sweet" "fruits"
## [10] "numwebvisitsmonth" "children" "gold"
## [13] "numwebpurchases" "acceptedcmp5" "acceptedcmp1"
## [16] "response" "acceptedcmp4" "numdealspurchases"
## [19] "acceptedcmp2" "age" "education"
## [22] "acceptedcmp3" "complain" "marital_status"
## [25] "recency"
The top 2 features are total_spent and income. So we will select these features to build the K-means model.
To study graphically which value of k gives us the best partition, we can plot betweenss and tot.withinss vs Choice of k.
bss <- numeric()
wss <- numeric()
# Run the algorithm for different values of k
set.seed(1234)
# set range of k value from 1 to 10
for(i in 1:10){
# For each k, calculate betweenss and tot.withinss
bss[i] <- kmeans(clean_fe_norm[c(5,13)], centers=i)$betweenss
wss[i] <- kmeans(clean_fe_norm[c(5,13)], centers=i)$tot.withinss
}
# Between-cluster sum of squares vs Choice of k
p3 <- qplot(1:10, bss, geom=c("point", "line"),
xlab="Number of clusters", ylab="Between-cluster sum of squares") +
scale_x_continuous(breaks=seq(0, 10, 1)) +
theme_bw()
# Total within-cluster sum of squares vs Choice of k
p4 <- qplot(1:10, wss, geom=c("point", "line"),
xlab="Number of clusters", ylab="Total within-cluster sum of squares") +
scale_x_continuous(breaks=seq(0, 10, 1)) +
theme_bw()
# Subplot
grid.arrange(p3, p4, ncol=2)
From the elbow plot above, it’s clear that the optimal number of cluster = 3. After K = 3, the difference between BCSS and WCSS value are not significant.
# Execution of k-means with k=3
set.seed(1234)
cust_k3 <- kmeans(clean_fe_norm[c(5,13)], centers=3, nstart = 20)
cust_k3
## K-means clustering with 3 clusters of sizes 635, 445, 1125
##
## Cluster means:
## income total_spent
## 1 0.5474790 0.30843020
## 2 0.6770414 0.62246032
## 3 0.2962515 0.04777425
##
## Clustering vector:
## 1 2 3 4 5 6 7 8 9 10 12 13 14 15 16 17
## 2 3 1 3 1 1 1 3 3 3 3 1 1 3 2 3
## 18 19 20 21 22 23 24 25 26 27 29 30 31 32 33 34
## 3 2 3 3 1 1 1 3 3 3 3 2 3 3 3 3
## 35 36 37 38 39 40 41 42 43 45 46 47 48 50 51 52
## 2 3 1 3 3 1 2 3 3 3 1 3 3 2 1 2
## 53 54 55 56 57 58 60 61 62 63 64 65 66 67 68 69
## 3 2 1 2 1 3 1 2 1 1 1 1 3 3 2 1
## 70 71 73 74 75 76 77 78 79 80 81 82 83 84 85 86
## 2 2 1 1 3 3 2 2 3 1 3 3 3 3 1 3
## 87 88 89 90 94 95 96 97 98 99 100 101 102 103 104 105
## 3 3 2 3 3 3 3 1 3 1 3 3 3 2 1 2
## 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
## 3 3 1 3 1 1 2 1 1 3 3 2 3 3 3 1
## 122 123 124 125 126 127 128 130 131 132 133 135 136 137 138 139
## 3 3 3 2 1 2 3 1 1 1 1 3 2 3 3 3
## 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
## 3 2 1 2 1 3 1 3 3 3 3 1 1 3 3 1
## 156 157 158 159 160 161 162 163 164 166 167 168 169 170 171 172
## 2 3 3 3 2 3 2 3 2 3 1 3 1 3 3 3
## 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
## 3 3 3 2 2 3 3 1 3 3 1 3 3 3 3 1
## 189 190 191 192 194 195 196 197 198 199 200 201 202 203 204 205
## 2 3 3 2 3 3 3 1 2 2 3 1 2 1 2 3
## 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
## 3 3 3 3 2 3 1 3 1 2 3 3 2 3 1 3
## 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
## 1 1 3 2 3 1 1 2 3 3 2 3 3 1 3 3
## 238 239 241 242 243 244 245 246 247 248 249 250 251 252 253 254
## 1 3 2 1 3 2 1 3 2 2 2 2 3 3 2 3
## 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
## 2 3 1 3 3 3 3 1 3 3 3 3 2 3 1 3
## 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
## 2 3 1 3 3 1 1 1 1 1 3 1 3 1 3 3
## 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
## 1 1 2 3 3 3 2 3 3 2 3 3 1 1 3 1
## 303 304 305 306 307 308 309 310 311 312 314 315 316 317 318 319
## 3 3 3 2 3 2 1 3 3 3 2 3 3 3 1 3
## 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
## 1 3 3 2 3 2 3 3 3 3 3 3 1 3 3 1
## 337 338 339 341 342 343 344 345 346 347 348 349 350 351 352 353
## 2 3 2 2 3 1 1 3 1 3 2 3 3 1 2 1
## 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
## 2 2 3 3 2 1 3 2 1 3 3 1 1 2 3 2
## 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
## 1 3 3 3 2 3 3 3 3 2 3 3 3 3 3 3
## 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
## 3 3 1 3 2 2 1 1 3 1 2 3 3 3 3 3
## 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
## 1 3 3 1 3 3 1 3 1 3 1 2 3 1 2 3
## 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
## 2 2 1 3 3 3 2 2 3 2 2 3 1 2 1 1
## 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
## 1 3 3 1 1 3 3 3 3 3 3 3 3 3 2 3
## 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465
## 1 1 1 3 1 1 3 2 3 3 2 1 1 3 2 3
## 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481
## 2 2 3 1 1 2 3 1 3 3 1 3 1 1 3 3
## 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497
## 3 3 3 1 1 1 1 3 3 2 3 2 1 1 1 3
## 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513
## 2 1 1 3 3 3 2 3 1 1 2 3 1 3 1 3
## 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529
## 2 3 2 3 3 1 2 3 2 3 2 3 3 2 1 2
## 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545
## 3 1 2 1 1 3 3 3 1 3 3 3 3 3 2 1
## 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561
## 3 2 3 3 3 3 3 1 3 1 3 1 2 3 2 3
## 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577
## 2 1 1 3 3 1 3 3 3 3 3 3 1 3 1 3
## 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593
## 3 3 3 3 3 3 3 3 3 2 1 1 3 3 2 2
## 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609
## 3 1 3 3 3 3 3 3 1 1 1 3 3 3 3 3
## 610 611 612 613 614 615 616 617 619 620 621 622 623 624 625 626
## 2 3 3 3 3 1 3 3 3 3 1 3 1 3 1 3
## 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642
## 2 2 3 3 2 1 1 3 2 3 2 1 2 2 1 2
## 643 644 645 646 647 648 649 650 651 652 653 654 655 657 658 659
## 2 3 2 3 2 3 1 1 2 3 1 3 3 3 3 1
## 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675
## 3 1 3 1 3 3 3 3 3 3 3 1 1 1 1 1
## 676 677 678 679 680 681 682 683 684 685 686 687 689 690 691 692
## 3 2 1 3 1 1 2 3 3 1 1 2 2 1 1 1
## 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708
## 3 3 3 3 3 3 1 1 1 1 2 2 3 2 3 1
## 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724
## 1 3 3 1 3 1 3 2 2 3 1 3 1 1 3 2
## 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740
## 3 3 2 1 1 1 3 1 1 3 2 2 2 1 3 2
## 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756
## 1 3 3 3 1 2 3 1 3 1 2 1 1 2 2 1
## 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772
## 1 1 3 3 3 1 2 3 1 3 2 2 3 1 1 2
## 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788
## 1 3 3 3 3 2 3 2 2 3 3 3 3 3 3 1
## 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804
## 1 3 2 1 3 3 3 3 1 1 2 3 1 3 3 2
## 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820
## 2 1 3 1 1 2 3 3 1 1 2 1 3 3 1 3
## 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836
## 2 3 1 3 2 2 2 3 2 3 3 1 1 3 3 1
## 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852
## 3 2 3 1 3 3 3 3 2 2 2 1 3 3 3 1
## 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868
## 2 3 3 1 3 1 3 2 3 3 3 3 3 1 3 3
## 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884
## 1 3 2 1 3 3 1 2 1 3 2 3 3 3 3 3
## 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900
## 2 2 3 3 1 2 3 3 1 3 1 1 1 2 3 3
## 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916
## 1 3 1 1 1 2 2 3 3 3 2 2 1 3 2 1
## 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932
## 1 2 3 2 3 2 3 3 2 3 2 2 1 2 2 3
## 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948
## 1 3 1 3 1 2 1 1 1 1 2 2 3 1 1 1
## 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964
## 3 1 3 3 3 3 3 3 1 1 3 1 1 1 3 3
## 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980
## 3 1 2 3 3 1 2 3 3 1 2 1 2 1 3 1
## 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996
## 3 3 3 1 2 1 2 2 2 3 2 3 3 1 3 3
## 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012
## 2 3 1 3 1 2 1 3 3 3 1 2 3 3 2 3
## 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028
## 3 3 3 1 2 3 3 3 3 3 2 3 3 1 3 3
## 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044
## 3 2 2 2 1 3 2 3 3 3 3 1 1 3 3 1
## 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060
## 3 3 3 2 3 1 2 3 2 3 3 1 3 3 2 1
## 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076
## 1 1 1 3 1 3 2 2 3 1 3 2 1 3 3 2
## 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092
## 2 3 3 3 1 3 2 3 2 1 3 1 3 2 2 3
## 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108
## 1 3 3 1 1 1 3 1 2 1 3 3 3 2 3 3
## 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124
## 1 1 2 2 3 2 3 2 3 3 3 1 1 3 3 3
## 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140
## 3 3 2 3 3 1 1 3 3 2 2 3 3 2 3 3
## 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156
## 1 3 3 3 1 3 3 1 1 3 1 2 3 2 3 3
## 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172
## 3 1 2 1 3 1 1 1 3 1 3 3 1 2 3 3
## 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188
## 2 1 3 3 3 1 3 2 1 3 1 3 3 3 3 1
## 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204
## 3 3 2 2 3 3 3 1 3 1 1 1 3 1 3 3
## 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220
## 1 1 2 3 3 3 3 1 1 2 1 3 1 3 2 3
## 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236
## 3 3 1 3 3 2 1 3 1 3 3 3 3 3 3 1
## 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252
## 3 2 3 3 3 3 1 1 3 3 3 3 3 1 2 1
## 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268
## 2 1 1 1 1 1 3 2 3 2 2 3 3 2 1 3
## 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284
## 3 2 1 1 3 1 3 2 3 3 2 3 2 1 3 3
## 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300
## 3 3 1 3 2 1 3 3 3 3 3 3 3 1 2 3
## 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317
## 2 3 3 2 1 3 2 1 1 1 2 3 1 1 3 3
## 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333
## 3 3 3 2 3 2 3 3 2 3 3 3 2 3 3 2
## 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349
## 2 1 3 1 3 3 3 3 3 1 3 3 3 3 3 1
## 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365
## 2 2 2 3 1 2 1 3 1 2 3 1 3 1 1 1
## 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1381 1382
## 2 3 3 3 1 3 3 3 1 1 3 1 3 1 3 3
## 1385 1386 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401
## 3 2 3 3 2 3 3 3 3 3 1 3 3 1 1 1
## 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417
## 1 3 3 1 3 3 1 1 3 1 2 3 2 3 1 3
## 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433
## 3 3 3 3 1 2 3 3 3 3 3 3 3 3 3 2
## 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449
## 3 1 3 1 3 3 3 3 3 3 2 2 3 2 1 2
## 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465
## 1 3 2 2 3 3 1 3 3 2 3 1 1 3 3 3
## 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481
## 1 1 2 3 2 3 3 3 2 1 3 2 3 3 1 1
## 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497
## 1 3 3 2 1 2 1 2 3 1 3 2 1 3 1 3
## 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513
## 1 2 2 1 3 3 1 2 1 1 1 1 1 2 3 2
## 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529
## 2 3 3 1 3 3 3 2 2 3 3 3 1 2 3 2
## 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545
## 3 2 3 1 3 3 3 3 1 1 1 3 1 2 3 1
## 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561
## 3 3 3 1 3 3 1 1 1 1 3 3 3 3 1 3
## 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577
## 1 3 1 3 3 2 1 1 1 2 3 2 3 3 3 3
## 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593
## 2 3 1 3 1 1 3 1 3 3 1 3 2 3 1 3
## 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609
## 3 3 3 3 3 2 3 1 2 2 3 3 3 1 1 1
## 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625
## 1 2 3 2 3 3 1 3 1 1 1 3 3 2 1 3
## 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641
## 3 1 3 3 3 1 3 3 3 3 1 1 1 3 3 3
## 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1655 1656 1657 1658
## 2 1 3 1 3 1 1 1 3 3 2 3 3 3 3 1
## 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674
## 1 2 1 3 1 3 3 3 1 3 2 2 3 2 2 1
## 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690
## 2 3 3 3 3 3 2 3 3 3 3 3 2 1 1 2
## 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706
## 1 2 3 3 3 1 3 1 3 2 1 3 3 1 3 3
## 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722
## 1 3 1 3 2 2 3 2 3 3 1 3 3 1 3 2
## 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738
## 2 2 3 3 3 3 1 2 3 3 3 2 1 2 1 2
## 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754
## 1 3 3 3 3 2 1 2 3 1 1 2 1 3 3 1
## 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770
## 3 3 3 3 3 1 1 3 1 1 3 1 3 1 3 3
## 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786
## 3 3 1 2 3 3 3 3 3 1 3 3 2 1 3 3
## 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802
## 1 3 1 1 3 3 2 3 3 3 1 3 1 1 2 2
## 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818
## 3 3 3 1 3 3 2 2 3 1 1 2 2 3 1 2
## 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834
## 1 3 1 3 3 1 1 3 1 2 2 3 3 3 1 3
## 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850
## 3 3 1 3 1 2 3 1 3 1 1 1 3 3 1 3
## 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866
## 2 3 2 2 2 2 3 3 2 1 1 3 1 1 2 1
## 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882
## 3 3 3 2 3 1 3 1 3 3 1 2 1 2 2 3
## 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898
## 3 1 3 1 3 2 1 3 2 2 3 1 1 3 3 2
## 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914
## 1 1 3 3 1 3 3 1 1 3 3 3 2 2 2 2
## 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930
## 1 1 3 3 3 3 3 1 2 2 2 3 1 2 2 1
## 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946
## 3 1 3 3 1 3 1 1 3 3 2 3 3 1 3 2
## 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962
## 1 1 3 3 3 1 2 2 1 2 3 3 2 3 1 2
## 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978
## 3 3 3 2 1 2 2 2 1 3 3 3 3 3 3 3
## 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994
## 3 3 3 3 2 3 1 3 3 3 3 1 2 3 2 2
## 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010
## 3 1 1 1 3 1 3 3 3 3 3 3 2 3 1 1
## 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026
## 2 3 2 1 2 3 3 3 3 3 3 3 3 2 2 3
## 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042
## 3 3 1 3 1 1 3 1 3 1 1 1 3 2 1 2
## 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058
## 3 3 3 3 3 1 2 2 3 1 1 3 3 3 2 1
## 2059 2061 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076
## 1 3 2 3 1 1 3 2 3 3 1 1 3 1 2 1
## 2077 2078 2081 2083 2084 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096
## 3 3 3 1 2 1 1 2 3 3 1 3 1 2 1 3
## 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112
## 2 3 2 2 3 1 3 2 3 3 3 1 3 2 1 1
## 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128
## 3 3 3 1 3 1 2 3 3 3 3 1 3 2 1 2
## 2129 2130 2131 2132 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145
## 2 3 3 2 3 1 1 3 3 1 3 3 3 3 3 1
## 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161
## 2 3 3 1 3 3 2 3 3 3 3 3 3 1 1 2
## 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177
## 1 3 2 3 3 2 2 2 3 1 1 1 2 1 2 2
## 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193
## 3 1 3 3 3 3 3 3 1 1 2 1 3 2 3 3
## 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209
## 2 2 3 3 1 1 3 3 1 3 1 3 3 1 3 3
## 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225
## 3 3 2 3 2 3 3 3 2 3 3 1 2 3 3 1
## 2226 2227 2228 2230 2231 2232 2233 2235 2236 2237 2238 2239 2240
## 1 1 1 3 3 1 3 3 2 1 1 1 3
##
## Within cluster sum of squares by cluster:
## [1] 11.377367 9.548749 14.938724
## (between_SS / total_SS = 82.2 %)
##
## Available components:
##
## [1] "cluster" "centers" "totss" "withinss" "tot.withinss"
## [6] "betweenss" "size" "iter" "ifault"
Additionally, the kmeans() function returns some ratios that let us know how compact is a cluster and how different are several clusters among themselves.
betweenss. The between-cluster sum of squares. In an optimal segmentation, one expects this ratio to be as higher as possible, since we would like to have heterogeneous clusters.
withinss. Vector of within-cluster sum of squares, one component per cluster. In an optimal segmentation, one expects this ratio to be as lower as possible for each cluster, since we would like to have homogeneity within the clusters.
tot.withinss. Total within-cluster sum of squares.
totss. The total sum of squares
A good clustering has a small WSS(k) and a large BSS(k).
# Between-cluster sum of squares
cust_k3$betweenss
## [1] 165.1532
# Within-cluster sum of squares
cust_k3$withinss
## [1] 11.377367 9.548749 14.938724
# Total within-cluster sum of squares / inertia value
cust_k3$tot.withinss
## [1] 35.86484
#Plot the data to see the clusters:
cust_k3$cluster <- as.factor(cust_k3$cluster)
ggplot(clean_fe, aes(income, total_spent, color = cust_k3$cluster)) + geom_point()
The bigger income of the customers, the more the total expenses.